import { http } from "@/utils/http";

// 派工管理查询参数接口
export interface WorkAssignmentQueryDto {
  page: number;
  limit: number;
  tenantId?: number;
  handler?: string;
  serviceAddress?: string;
  keyword?: string; // 用于搜索处理人、服务地址或要求处理事项
  completionStatus?: string;
}

// 添加派工信息参数接口
export interface WorkAssignmentAddDto {
  tenantId?: number; // 后端自动获取
  repairReportId?: number;
  assignmentTime: string;
  handler: string;
  totalFee: number;
  serviceAddress: string;
  requiredTasks: string;
  assignmentNotes?: string;
  completionTime?: string;
  completionStatus?: string;
}

// 更新派工信息参数接口
export interface WorkAssignmentUpdateDto {
  id: number;
  tenantId?: number; // 后端自动获取
  assignmentTime?: string;
  handler?: string;
  totalFee?: number;
  serviceAddress?: string;
  requiredTasks?: string;
  assignmentNotes?: string;
  completionTime?: string;
  completionStatus?: string;
}

// 派工回单参数接口
export interface WorkAssignmentReceiptDto {
  repairReportId: number; // 报事报修ID
  handlingSituation: string; // 处理情况
  remarks?: string; // 备注
}

// 派工管理列表项接口
export interface WorkAssignmentListVo {
  id?: number;
  tenantId?: number;
  repairReportId?: number;
  assignmentTime?: string;
  handler?: string;
  totalFee?: number;
  serviceAddress?: string;
  requiredTasks?: string;
  assignmentNotes?: string;
  completionTime?: string;
  completionStatus?: string;
  createTime?: string;
  updateTime?: string;
}

// 派工管理分页结果接口
export interface WorkAssignmentListResult {
  code: number;
  data: {
    total: number;
    list: WorkAssignmentListVo[];
    pageNum: number;
    pageSize: number;
    size: number;
    startRow: number;
    endRow: number;
    pages: number;
    prePage: number;
    nextPage: number;
    isFirstPage: boolean;
    isLastPage: boolean;
    hasPreviousPage: boolean;
    hasNextPage: boolean;
    navigatePages: number;
    navigatepageNums: number[];
    navigateFirstPage: number;
    navigateLastPage: number;
  };
  message: string;
}

// 派工管理详情结果接口
export interface WorkAssignmentDetailResult {
  code: number;
  data: WorkAssignmentListVo;
  message: string;
}

// 操作结果接口
export interface WorkAssignmentOperationResult {
  code: number;
  data: boolean;
  message: string;
}

// 获取派工列表
export const getWorkAssignmentList = (params: WorkAssignmentQueryDto) => {
  // 确保参数结构正确，特别是分页参数
  const requestParams = {
    page: params.page || 1,
    limit: params.limit || 10,
    tenantId: params.tenantId,
    // 不再单独传递handler参数，通过keyword统一搜索
    serviceAddress: params.serviceAddress,
    keyword: params.keyword,
    completionStatus: params.completionStatus
  };
  
  console.log("API请求参数:", requestParams);
  
  return http.request<WorkAssignmentListResult>("post", "/workAssignment/list", {
    data: requestParams
  });
};

// 根据ID获取派工详情
export const getWorkAssignmentById = (id: number) => {
  return http.request<WorkAssignmentDetailResult>("get", `/workAssignment/find/${id}`);
};

// 添加派工信息
export const addWorkAssignment = (params: WorkAssignmentAddDto) => {
  return http.request<WorkAssignmentOperationResult>("post", "/workAssignment/add", {
    data: params
  });
};

// 更新派工信息
export const updateWorkAssignment = (params: WorkAssignmentUpdateDto) => {
  return http.request<WorkAssignmentOperationResult>("post", "/workAssignment/update", {
    data: params
  });
};

// 删除派工信息
export const deleteWorkAssignment = (id: number) => {
  return http.request<WorkAssignmentOperationResult>("get", `/workAssignment/delete/${id}`);
};

// 提交派工回单
export const submitReceipt = (params: WorkAssignmentReceiptDto) => {
  return http.request<WorkAssignmentOperationResult>("post", "/workAssignment/receipt", {
    data: params
  });
};